<template>
  <div>
    <!-- 货运单查询——货运单页面-->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange" ></TopFilter>
    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        columnsRef="shipmentOrderListIndex"
        :isSelectColumn="true"
        :onRowSelectChange="selectChangeHandle"
        :onColumnsChange="(columns) => (this.columns = columns)"
      >
        <template slot="controls">
          <multiuse-button size="small" type="primary" @click="addHandle(1)" :disabled="isPrintOrder">打 印</multiuse-button>
          <multiuse-button size="small" type="primary" @click="addHandle(2)" :disabled="ischange">发 运</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <Drawer :visible.sync="detailVisible" :title="'货运单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <detailModel :rowData="rowData" @close="detailcloseHandler" />
    </Drawer>
     <BasePrint ref="printPick" :data="printData" template="templateShipmentOrder" />
  </div>
</template>
<script>
import { getShipmentOrderList, deleteShipmentOrder , passShipmentOrder, queryShipmentOrderById} from '@/api/omSale/OrderManagement/ShipmentOrderQuery';
import detailModel from './detailModel';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'shipmentOrderListIndex',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {},
      fetchApi: getShipmentOrderList,
      rowData: {},
      exportLoading: false,
      detailVisible: false,
      ischange: true,
      isPrintOrder: true,
      selectList: [],
      printData: {}
    };
  },
  components: {
    detailModel,
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {},
  methods: {
    async addHandle(type) {
      if (type == 1) {
        console.log('打印');
        this.isPrintOrder = true;
        let res = await queryShipmentOrderById({shipmentOrderId: this.selectList[0].shipmentOrderId});
        if (res.resultCode == 200) {
          this.printData = res.data;
          this.$refs.printPick.EXCUTE_PRINT();
        }
      } else {
        this.checkShipmentOrders();
        console.log('校验',this.ischange);
        if (this.ischange == false) {
          let shipmentOrderIdes = this.selectList.map(x => x.shipmentOrderId);
          console.log('发运',shipmentOrderIdes);
          await this.$confirm('是否发运选中货运单?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
            .then(async () => {
              this.ischange = true;
              let res = await passShipmentOrder({shipmentOrderIdes: shipmentOrderIdes});
              if (res.resultCode == 200) {
                this.$notify({
                  title: res.data,
                  message: res.errMsg,
                  type: 'success'
                });
                this.ischange = false;
                this.fetchParams = Object.assign({}, this.fetchParams);
              }
            })
            .catch(() => {});
        }
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async editHandle(data, type) {
      if (type === 1) {
        this.rowData = data;
        this.detailVisible = true;
      } else {
        console.log('删除货运单', data);
        await this.$confirm('是否确定删除货运单?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let res = await deleteShipmentOrder({shipmentOrderId: data.shipmentOrderId});
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '货运单删除成功',
                type: 'success'
              });
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      } 
    },
    checkShipmentOrders() {
      for (let SelectData of this.selectList) {
        if(SelectData.isShipment != 10041002) {
          this.ischange = true;
          this.$notify({
          title: '警告',
          message: "存在已发运状态的货运单，无法再次发运",
          type: 'warning',
          });
          return ;
        }
      }
    },
    selectChangeHandle(row) {
      this.selectList = row;
      console.log('选中数据',this.selectList);
      if (this.selectList.length > 0) { 
        this.ischange = false;
        if (this.selectList.length == 1) {
          this.isPrintOrder = false;
        } else {
          this.isPrintOrder = true;
        }
      } else {
        this.ischange = true;
        this.isPrintOrder = true;
      }
    },
    detailcloseHandler() {
      this.detailVisible = false;
    },
    resetChange(val) {
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: '货运单号',
          fieldName: 'shipmentOrderNo',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '是否发运',
          fieldName: 'isShipment',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
        },
        {
          type: 'SELECT',
          label: '是否打印',
          fieldName: 'isPrint',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
        },
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: (props) => {
            return <span>{props.row.index + 1}</span>;
          },
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 1)}>
                  详情
                </multiuse-button>
                 <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)} disabled = {props.row.isShipment == 10041001}>
                  删除
                </multiuse-button>
              </div>
            );
          },
        },
        {
          title: '货运单号',
          dataIndex: 'shipmentOrderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '是否发运',
          dataIndex: 'isShipment',
          sorter: true,
          filter: true,
          filterType: 'input',
          filterItems: this.createDictList('1004'),
          render: (p) => {
            return <span>{this.createDictText(p.row.isShipment, '1004')}</span>;
          },
        },
        {
          title: '是否打印',
          dataIndex: 'isPrint',
          sorter: true,
          filter: true,
          filterType: 'input',
          filterItems: this.createDictList('1004'),
          render: (p) => {
            return <span>{this.createDictText(p.row.isPrint, '1004')}</span>;
          },
        },
        {
          title: '省份',
          dataIndex: 'provinceName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '城市',
          dataIndex: 'cityName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },      
        {
          title: '发车基地',
          dataIndex: 'carrierBase',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '收车地址',
          dataIndex: 'address',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '送达城市',
          dataIndex: 'deliveryCityName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '联系人',
          dataIndex: 'linkMan',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '联系电话',
          dataIndex: 'linkPhone',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '物流公司',
          dataIndex: 'carrierCompany',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
         {
          title: '车牌号',
          dataIndex: 'trailerBrand',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '板次',
          dataIndex: 'frequency',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '驾驶员姓名',
          dataIndex: 'carrierDriver',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '驾驶员电话',
          dataIndex: 'carrierDriverPhone',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '驾驶员身份证号',
          dataIndex: 'carrierDriverIdCard',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '发车时间',
          dataIndex: 'carrierDate',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '收车人',
          dataIndex: 'receiver',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '收车时间',
          dataIndex: 'receiveDate',
          sorter: true,
          filter: true,
          filterType: 'input',
        }
      ];
    },
  },
};
</script>